package rs.etf.es.algorithm;

import java.util.EmptyStackException;
import java.util.Stack;

public class MementoCaretaker {

    private static MementoCaretaker mementoCaretaker;

    public static MementoCaretaker instance() {
	if (mementoCaretaker == null) {
	    mementoCaretaker = new MementoCaretaker();
	}

	return mementoCaretaker;
    }

    private MementoCaretaker() {

    }

    private Stack<AlgorithmMemento> steps = new Stack<AlgorithmMemento>();
    private AlgorithmMemento initialMemento;

    public void pushCurrentMemento(AlgorithmMemento memento) {
	steps.push(memento);
    }

    public AlgorithmMemento oneStepBack() {

	AlgorithmMemento tmp;
	try {
	    steps.pop();
	    tmp = steps.pop();
	} catch (EmptyStackException e) {
	    return null;
	}

	return tmp;

    }

    public AlgorithmMemento getInitialMemento() {
	return initialMemento;
    }

    public void setInitialMemento(AlgorithmMemento initialMemento) {
	this.initialMemento = initialMemento;
    }

    public void emptyStack() {
	steps.clear();
    }

}
